package functions;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import twitter4j.internal.org.json.JSONArray;
import twitter4j.internal.org.json.JSONException;

public class StaticFunctions {

	/**
	 * Changes a JSONArray into an ArrayList<String>
	 * 
	 * @param jsonArray
	 *            The array you want to change
	 * @return ArrayList<String> The values from the JSONArray in an
	 *         ArrayList<String>
	 */
	public static ArrayList<String> JSONArrayToArrayList(JSONArray jsonArray) {
		ArrayList<String> values = new ArrayList<String>();
		for (int i = 0; i < jsonArray.length(); i++) {
			try {
				values.add((String) jsonArray.get(i));
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		return values;
	}

	/**
	 * Returns a date array based upon the given granularity and amount from
	 * now.
	 * 
	 * @param granularity
	 * @param amount
	 * @return ArrayList<String>
	 */
	public static ArrayList<String> getDateArray(String granularity, int amount) {

		ArrayList<String> dateArray = new ArrayList<String>();

		Calendar today = Calendar.getInstance();
		if (granularity.equals("MINUTE")) {
			today.add(Calendar.MINUTE, -amount);
			for (int i = 0; i < amount; i++) {
				today.add(Calendar.MINUTE, +1);
				java.sql.Timestamp lastdate = new java.sql.Timestamp(
						today.getTimeInMillis());
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd H:m");
				dateArray.add(sdf.format(lastdate));
			}
		} else if (granularity.equals("HOUR")) {
			today.add(Calendar.HOUR, -amount);
			for (int i = 0; i < amount; i++) {
				today.add(Calendar.HOUR, +1);
				java.sql.Timestamp lastdate = new java.sql.Timestamp(
						today.getTimeInMillis());
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd H");
				dateArray.add(sdf.format(lastdate));
			}
		} else if (granularity.equals("DAY")) {
			today.add(Calendar.DATE, -amount);
			for (int i = 0; i < amount; i++) {
				today.add(Calendar.DATE, +1);
				java.sql.Date lastdate = new java.sql.Date(
						today.getTimeInMillis());
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				dateArray.add(sdf.format(lastdate));
			}
		} else if (granularity.equals("WEEK")) {
			today.add(Calendar.DATE, (7 * -amount));
			for (int i = 0; i < amount; i++) {
				today.add(Calendar.DATE, (7 * 1));
				java.sql.Date lastdate = new java.sql.Date(
						today.getTimeInMillis());
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-w");
				dateArray.add(sdf.format(lastdate));
			}
		} else if (granularity.equals("MONTH")) {
			today.add(Calendar.MONTH, -amount);
			for (int i = 0; i < amount; i++) {
				today.add(Calendar.MONTH, +1);
				java.sql.Date lastdate = new java.sql.Date(
						today.getTimeInMillis());
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
				dateArray.add(sdf.format(lastdate));
			}
		} else if (granularity.equals("YEAR")) {
			today.add(Calendar.YEAR, -amount);
			for (int i = 0; i < amount; i++) {
				today.add(Calendar.YEAR, +1);
				java.sql.Date lastdate = new java.sql.Date(
						today.getTimeInMillis());
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
				dateArray.add(sdf.format(lastdate));
			}
		}
		return dateArray;
	}
}
